package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    public Result<PageResult> SetmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询参数为 : {}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageSearch(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result getDishById(@PathVariable Integer id) {
        log.info("分页查询参数为 : {}", id);
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result insertDish(@RequestBody SetmealDTO setmealDTO) throws Exception {
        log.info("添加参数为 : {}", setmealDTO);
        setmealService.insertSetmeal(setmealDTO);
        return Result.success();
    }
//
//    @GetMapping("/list")
//    public Result<List<Dish>> dishList(Integer categoryId) {
//        log.info("查询菜品分类,参数为: {}", categoryId);
//        List<Dish> dishes = setmealService.categoryIdByList(categoryId);
//        return Result.success(dishes);
//    }
//
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result stopOrStartSetmeal(@PathVariable Integer status, Long id) {
        log.info("起售或停售,参数为: {} {}", status, id);
        setmealService.statusSetmeal(status, id);
        return Result.success();
    }


    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result updateSetmealWithSetmealDishes(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改菜品,参数为为: {}", setmealDTO);
        setmealService.updateSetmealWithSetmealDishes(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result deleteDishes(@RequestParam("ids") List<Long> ids) {
        log.info("删除套餐,参数为为: {}", ids);
        if (ids == null || ids.size() == 0) {
            return Result.error("请选中需要删除的菜品");
        }
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }

}
